﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DienLib.ReportLib
{
    /// <summary>
    /// Interaction logic for PopUpWellType.xaml
    /// </summary>
    public partial class PopUpShipTo : UserControl
    {
        string wellname;
        int current;
        private PopUpShipTo(string WellName, DienLib.ReportLib.ShipTo[] ShipTos)
        {
            InitializeComponent();
            this.wellname = WellName;
            this.textBlock2.Text += WellName;
            //comboBox1.DisplayMemberPath = "Ship2Number";
            if (ShipTos != null)
            {
                comboBoxD1.ItemsSource = ShipTos; // ShipTo Combobox
            }
        }

        /// <summary>
        /// Additional Method from User to Get Value back from PopUp
        /// </summary>
        /// <returns>String contain ShipTo Number</returns>
        public static string ShowDialog(string WellName, DienLib.ReportLib.ShipTo[] ShipTos = null)
        {
            /*System.Windows.Controls.Primitives.Popup popup = new System.Windows.Controls.Primitives.Popup();
            popup.IsOpen = true;
            popup.StaysOpen = false;*/

            PopUpShipTo popup = new PopUpShipTo(WellName, ShipTos);
            Window window = new Window();
            window.Content = popup;
            window.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
            window.WindowStyle = System.Windows.WindowStyle.None;
            window.AllowsTransparency = true;
            window.Background = System.Windows.Media.Brushes.Transparent;
            window.Topmost = true;
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.MouseLeftButtonDown += Window_MouseLeftButtonDown;
            System.Media.SystemSounds.Exclamation.Play();
            window.ShowDialog(); // ShowDialog can be used Internal ? HA HA HA ! Any Side Effect ?
            window.MouseLeftButtonDown -= Window_MouseLeftButtonDown;
            return popup.comboBoxD1.Text;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try { Window.GetWindow(this).Close(); }
            catch (Exception ex) { throw new InvalidOperationException("Cannot close Parent not Window : " + ex.Message); }
        }

        private static void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            (sender as System.Windows.Window).DragMove();
        }

        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {   // Khong co dieu gi xay ra : Co the chay chu tren Text ????
            //ShipTo ship2 = comboBox1.SelectedItem as ShipTo;
            //if (ship2 == null) return;

            /////////////////////////////////////////////////////////////////////////////////////////////////////////////comboBox1.Text = ship2.Ship2Number;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // Search & Set Default SHIP TO for that WELL if FOUND.
            int duplicate = -1;
            for (int i = 0; i < comboBoxD1.Items.Count; i++)
            {
                ShipTo item = comboBoxD1.Items[i] as ShipTo;
                if (item.WellName.Replace(" ", "").ToLower() == wellname.Replace(" ", "").ToLower())
                {   // Founded => Set this Default
                    comboBoxD1.SelectedIndex = current = i; // Only add .ShipTo to combo.Text because not override SelectedChange
                    //comboBox1.Text = item.Ship2Number;
                    duplicate++;
                }
            }

            if (duplicate < 0) PopUp.PopUpOK.ShowDialog("Check your \"Config File\", No ShipTo for this Well in \"ShipTo\" worksheet",
                string.Format("NO SHIPTO for WELL : \"{0}\"", wellname));
            else if (duplicate > 0) PopUp.PopUpOK.ShowDialog("Check your \"Config File\", Duplicate WellName in \"ShipTo\" worksheet",
                string.Format("DUPLICATE WELLNAME : \"{0}\"", wellname));

        }

        private void button1_MouseEnter(object sender, MouseEventArgs e)
        {
            comboBoxD1.FormatSelectionBox();
        }

        private void button1_MouseLeave(object sender, MouseEventArgs e)
        {
            //comboBoxD1.ReloadSelectionBox();
        }
    }
}
